package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import com.zp.drug.entity.DrugClassDict;
import com.zp.drug.entity.bo.DrugClassDictBo;
import com.zp.drug.entity.vo.DrugClassDictVo;
import com.zp.drug.mapper.DrugClassDictMapper;
import com.zp.drug.service.IDrugClassDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 药品功效类别字典Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-05-31
 */
@RequiredArgsConstructor
@Service
public class DrugClassDictServiceImpl implements IDrugClassDictService {

    private final DrugClassDictMapper baseMapper;

    /**
     * 查询药品功效类别字典
     */
    @Override
    public DrugClassDictVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药品功效类别字典列表(分页)
     */
    @Override
    public TableDataInfo<DrugClassDictVo> queryPageList (DrugClassDictBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DrugClassDict> lqw = buildQueryWrapper(bo);
        Page<DrugClassDictVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品功效类别字典列表(不分页)
     */
    @Override
    public List<DrugClassDictVo> queryList (DrugClassDictBo bo) {
        LambdaQueryWrapper<DrugClassDict> lqw = buildQueryWrapper(bo);
        List<DrugClassDictVo> drugClassDictVos = baseMapper.selectVoList(lqw);
        setDrugClassCache(drugClassDictVos);
        return drugClassDictVos;
    }

    private LambdaQueryWrapper<DrugClassDict> buildQueryWrapper (DrugClassDictBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrugClassDict> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getClassCode()), DrugClassDict::getClassCode, bo.getClassCode());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()), DrugClassDict::getClassName, bo.getClassName());
        lqw.eq(StringUtils.isNotBlank(bo.getParentId()), DrugClassDict::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getParentIds()), DrugClassDict::getParentIds, bo.getParentIds());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), DrugClassDict::getRemarks, bo.getRemarks());
        lqw.orderByAsc(DrugClassDict::getSort);
        return lqw;
    }

    /**
     * 新增药品功效类别字典
     */
    @Override
    public Boolean insertByBo (DrugClassDictBo bo) {
        DrugClassDict add = BeanUtil.toBean(bo, DrugClassDict.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        add.initEntity();
        // 添加缓存
        CacheUtils.put(CacheNames.DRUG_CLASS_DICT,add.getClassCode(),add);
        return flag;
    }

    private void setDrugClassCache (List<DrugClassDictVo> drugClassDictVos) {
        drugClassDictVos.forEach(item->{
            CacheUtils.put(CacheNames.DRUG_CLASS_DICT,item.getClassCode(),item);
        });
    }

    /**
     * 修改药品功效类别字典
     */
    @Override
    public Boolean updateByBo (DrugClassDictBo bo) {
        DrugClassDict update = BeanUtil.toBean(bo, DrugClassDict.class);
        validEntityBeforeSave(update);
        CacheUtils.evict(CacheNames.DRUG_CLASS_DICT,bo.getClassCode());
        CacheUtils.put(CacheNames.DRUG_CLASS_DICT,bo.getClassCode(),bo);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (DrugClassDict entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品功效类别字典
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        boolean b = baseMapper.deleteBatchIds(ids) > 0;
        ids.forEach(item->{
            DrugClassDict drugClassDict = baseMapper.selectById(item);
            CacheUtils.evict(CacheNames.DRUG_CLASS_DICT,drugClassDict.getClassCode());
        });
        return true;
    }
}
